<?php
class login extends api_controllor 
{
    private $_username;
    private $_password;
    private $oSess;
    
    public function __construct($get_param)
    {
        $this->__need_login = false;
        parent::__construct($get_param);
    }
    
    public function run()
    {
        $this->check_para();
        
        $oMember = K::load_mod('member');
        $oMember->login($this->_username , $this->_password )
            or $this->busy('login');
            
        $aUserinfo = $oMember->get_data();
        
        if(!$aUserinfo || !$aUserinfo['uid'])
        {
            $this->oSess->set('isNeedVcode' , 1);
            $this->redirect('/login/vcode');
        }
        elseif($aUserinfo['stat'] != K_COMMON_YES)
        {
            $this->oSess->set('isNeedVcode' , 1);
            $this->redirect('/login/deny');
        }
        else 
        {
            $this->oSess->delete('isNeedVcode');
            
            //初始化SESSION
            $this->oSess->set(K_SESSION_KEY_LOGIN , $aUserinfo);
            
            setcookie(K_COOKIE_JS_UID , $aUserinfo['uid'] , time()+864400 , '/' , K5_DOMAIN);
            
            $oMember->update_logintime($aUserinfo['uid']);
            
            //语言
	        if(in_array($_COOKIE[K_COOKIE_LANG] , array(K_LANG_CHN , K_LANG_ENG)))
	        {
	            $this->__isLangEng = $_COOKIE[K_COOKIE_LANG] == K_LANG_ENG ? true : false;
	            $L = K::config($_COOKIE[K_COOKIE_LANG]);
	        }
	        else 
	        {
	            $this->__isLangEng = K_DEFAULT_LANG == K_LANG_ENG ? true : false;
	            $L = K::config(K_DEFAULT_LANG);
	        }
	        echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
            echo '<script>alert("'.$L['pLoginAlert'].'");</script>';
            $this->redirect('/navi',1);
        }
    }
    
    public function check_para()
    {
        $this->oSess = K::session();
        $this->oSess->get('isNeedVcode' , $isNeedVcode);
        if($isNeedVcode == 1)
        {
            $vcode = $this->input('vcode');
            
            if(!$this->oSess->check_validate_code($vcode))
                $this->redirect('/login/vcode');
        }
        
        $this->_username = $this->input('username');
        $this->_password = $this->input('password');
        
        if(!preg_match('/^[a-zA-Z0-9_]{5,20}$/' , $this->_username))
            $this->redirect('/login/uname');
        elseif(!$this->_password) 
            $this->redirect('/login/pw');
    }
}
?>